package br.com.webhomesites.locacao.presenter;

import br.com.webhomesites.locacao.contracts.IEmpresaManutencaoView;
import br.com.webhomesites.padrao.utils.Mascarador;
import java.awt.Component;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import javax.swing.JOptionPane;

public class EmpresaManutencaoPresenterActionListeners {

    static class AlterarActionListener implements ActionListener {

        private EmpresaManutencaoPresenter presenter;

        public AlterarActionListener(EmpresaManutencaoPresenter presenter) {
            this.presenter = presenter;
        }

        @Override
        public void actionPerformed(ActionEvent evt) {
            presenter.setOperacao(presenter.UPDATE_STRATEGY);
            presenter.habilitarEdicao(true);


            IEmpresaManutencaoView view = (IEmpresaManutencaoView) presenter.getView();

            view.enableBtnInserir(true);
            view.enableBtnRemover(true);
            view.enableBtnCancelar(true);
            view.enableBtnSalvar(true);
            view.enableBtnAlterar(true);

        }
    }

    static class CancelarActionListener implements ActionListener {

        private EmpresaManutencaoPresenter presenter;

        public CancelarActionListener(EmpresaManutencaoPresenter presenter) {
            this.presenter = presenter;
        }

        @Override
        public void actionPerformed(ActionEvent evt) {

            IEmpresaManutencaoView view = (IEmpresaManutencaoView) presenter.getView();

            view.clearFields();

            //presenter.habilitarEdicao(false);  

            view.enableBtnInserir(true);
            view.enableBtnRemover(false);
            view.enableBtnCancelar(false);
            view.enableBtnSalvar(false);
            view.enableBtnAlterar(false);

        }
    }

    static class SalvarActionListener implements ActionListener {

        private EmpresaManutencaoPresenter presenter;

        public SalvarActionListener(EmpresaManutencaoPresenter presenter) {
            this.presenter = presenter;
        }

        @Override
        public void actionPerformed(ActionEvent evt) {
            presenter.getOperacao().execute();
            //presenter.setOperacao(presenter.UPDATE_STRATEGY);
            //presenter.habilitarEdicao(false);  

            //setUpViewListeners();

            IEmpresaManutencaoView view = (IEmpresaManutencaoView) presenter.getView();

            if (EmpresaManutencaoPresenter.validado) {
                view.enableBtnInserir(true);
                view.enableBtnRemover(false);
                view.enableBtnCancelar(false);
                view.enableBtnSalvar(false);
                view.enableBtnAlterar(false);

                System.out.println("Passou na ação salvar!");
            }

        }
    }

    static class InserirActionListener implements ActionListener {

        private EmpresaManutencaoPresenter presenter;

        public InserirActionListener(EmpresaManutencaoPresenter presenter) {
            this.presenter = presenter;
        }

        @Override
        public void actionPerformed(ActionEvent evt) {
            presenter.setOperacao(presenter.INSERT_STRATEGY);


            IEmpresaManutencaoView view = (IEmpresaManutencaoView) presenter.getView();

            presenter.habilitarEdicao(true);
            view.clearFields();

            presenter.novoEmpresaManutencao();


            view.enableBtnInserir(false);
            view.enableBtnRemover(false);
            view.enableBtnCancelar(true);
            view.enableBtnSalvar(true);
            view.enableBtnAlterar(false);

        }
    }

    static class RemoverActionListener implements ActionListener {

        private EmpresaManutencaoPresenter presenter;

        public RemoverActionListener(EmpresaManutencaoPresenter presenter) {
            this.presenter = presenter;
        }

        @Override
        public void actionPerformed(ActionEvent evt) {

            if (JOptionPane.showConfirmDialog((Component) presenter.getView(), "Deseja remover?", "Pergunta", JOptionPane.YES_NO_OPTION, JOptionPane.QUESTION_MESSAGE) == JOptionPane.YES_OPTION) {

                IEmpresaManutencaoView view = (IEmpresaManutencaoView) presenter.getView();

                presenter.removerEmpresaManutencao();
                view.clearFields();

                view.enableBtnInserir(true);
                view.enableBtnAlterar(false);
                view.enableBtnRemover(false);
            }
        }
    }

    static class SairActionListener implements ActionListener {

        @Override
        public void actionPerformed(ActionEvent evt) {
            System.exit(0);
        }
    }  
    
    static class CampoKeyListener implements KeyListener {

        private EmpresaManutencaoPresenter presenter;
        IEmpresaManutencaoView view;
        Mascarador mask;

        public CampoKeyListener(EmpresaManutencaoPresenter presenter) {
            this.presenter = presenter;
            this.view = (IEmpresaManutencaoView) presenter.getView();
            mask = new Mascarador();
        }

        @Override
        public void keyTyped(KeyEvent e) {
            //throw new UnsupportedOperationException("Not supported yet.");
        }

        @Override
        public void keyPressed(KeyEvent e) {
            //throw new UnsupportedOperationException("Not supported yet.");
        }

        @Override
        public void keyReleased(KeyEvent e) {
            view.setCpf(mask.getMascara(view.getCpf(), mask.CPF, e));
            view.setCnpj(mask.getMascara(view.getCnpj(), mask.CNPJ, e));
            view.setRg(mask.getMascara(view.getRg(), mask.RG, e));
            view.setTelefone(mask.getMascara(view.getTelefone(), mask.TELEFONE, e));
            view.setCelular(mask.getMascara(view.getCelular(), mask.TELEFONE, e));
        }
    }

    static class ItemChangedListener implements ActionListener {

        private EmpresaManutencaoPresenter presenter;
        IEmpresaManutencaoView view;

        public ItemChangedListener(EmpresaManutencaoPresenter presenter) {
            this.presenter = presenter;
            this.view = (IEmpresaManutencaoView) presenter.getView();
        }

        @Override
        public void actionPerformed(ActionEvent e) {
//            if (view.isRbPessoaFisica()) {
//                view.enableTxtClienteRg(true);
//                view.enableTxtClienteCpf(true);
//                view.enableTxtClienteCnpj(false);
//                view.enableTxtClienteInscricaoEstadual(false);
//                view.enableTxtClienteRazaoSocial(false);
//            } else if (view.isRbPessoaJuridica()) {
//                view.enableTxtClienteRg(false);
//                view.enableTxtClienteCpf(false);
//                view.enableTxtClienteCnpj(true);
//                view.enableTxtClienteInscricaoEstadual(true);
//                view.enableTxtClienteRazaoSocial(true);
//            }
        }
    }
}
